package com.freedom.org.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.freedom.auth.vo.ClueExportVo;
import com.freedom.auth.vo.EmployeeExportVo;
import com.freedom.base.annotation.FreedomPermission;
import com.freedom.base.context.LoginContext;
import com.freedom.org.domain.Department;
import com.freedom.org.service.IEmployeeService;
import com.freedom.org.domain.Employee;
import com.freedom.org.query.EmployeeQuery;
import com.freedom.base.util.AjaxResult;
import com.freedom.base.util.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/emp")
@FreedomPermission(name = "员工管理")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     *
     * @param employee 传递的实体
     * @return AjaxResult转换结果
     */
    @PostMapping
    @FreedomPermission(name = "员工新增修改")
    public AjaxResult insertOrUpdate ( @RequestBody Employee employee ) {
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
                return AjaxResult.success("修改成功");
            } else {
                employeeService.insert(employee);
                return AjaxResult.success("新增成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    @FreedomPermission(name = "员工删除")
    public AjaxResult delete ( @PathVariable("id") Serializable id ) {
        try {
            employeeService.deleteById(id);
            return AjaxResult.success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("删除失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @FreedomPermission(name = "获取员工")
    public AjaxResult getById ( @PathVariable("id") Serializable id ) {
        try {
            Employee employee = employeeService.getById(id);
            return AjaxResult.success(employee, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    @FreedomPermission(name = "获取所有员工")
    public AjaxResult getAll () {
        try {
            List<Employee> list = employeeService.getAll();
            return AjaxResult.success(list, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PutMapping
    @FreedomPermission(name = "员工分页查询")
    public AjaxResult queryPage ( @RequestBody EmployeeQuery query ) {
        try {
            PageInfo<Employee> pageInfo = employeeService.queryPage(query);

            LoginContext.queryMap.put("query", query);


            return AjaxResult.success(pageInfo, "分页查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("分页查询失败！" + e.getMessage());
        }
    }
    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
//    @PutMapping("/exportExcel")
//    @FreedomPermission(name = "员工分页查询")
//    public AjaxResult exportExcel ( @RequestBody EmployeeQuery query ) {
//        try {
//            PageInfo<Employee> pageInfo = employeeService.queryPage(query);
//            List<Employee> data = pageInfo.getData();
//
//
//            ExportParams params = new ExportParams("员工表", "sheet", ExcelType.XSSF);
//
//            Workbook workbook = ExcelExportUtil.exportExcel(params, EmployeeExportVo.class, data);
//
//
//            FileOutputStream fos = new FileOutputStream("C:\\Users\\freedom\\Desktop\\emp.xlsx");
//
//            workbook.write(fos);
//            fos.close();
//
//            return AjaxResult.success(pageInfo, "分页查询成功");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return AjaxResult.error("分页查询失败！" + e.getMessage());
//        }
//    }

    /**
     * 批量删除
     */
    @PatchMapping
    @FreedomPermission(name = "员工批量删除")
    public AjaxResult batchDelete ( @RequestBody List<Long> ids ) {
        try {
            employeeService.batchDelete(ids);
            return AjaxResult.success("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败");
        }
    }

    /**
     * 查询所属部门
     */
    @GetMapping("/parent")
    @FreedomPermission(name = "查询上级部门")
    public AjaxResult getParents () {
        try {
            List<Employee> parents = employeeService.getParents();
            return AjaxResult.success(parents, "查询成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("查询失败");
        }
    }


    @PostMapping("/importExcel")
    public AjaxResult importExcel ( @RequestParam("file") MultipartFile file ) {
        try {
            String result = employeeService.importExcel(file);
            return AjaxResult.success(result, "导入成功");
        } catch (Exception e) {
            return AjaxResult.error("导入失败");
        }
    }

    @PostMapping("/exportExcel")
    public AjaxResult exportExcel () {
        try {
            List<EmployeeExportVo> list = employeeService.getClueExportList();


            ExportParams params = new ExportParams("员工表", "sheet", ExcelType.XSSF);

            Workbook workbook = ExcelExportUtil.exportExcel(params, EmployeeExportVo.class, list);


            FileOutputStream fos = new FileOutputStream("C:\\Users\\freedom\\Desktop\\test\\emp.xlsx");

            workbook.write(fos);
            fos.close();

            return AjaxResult.success("导出成功");
        } catch (Exception e) {
            return AjaxResult.error("导出失败");
        }
    }

}
